<?php
/**
 * User:chendenan
 * Created On 2022/3/9 0009 20:57
 */

namespace app\admin\business;
use app\common\model\mysql\AdminUser as AdminUserModel;
class AdminUser{
    public $adminUserObj = null ;
    public function __construct(){
        $this->adminUserObj = new AdminUserModel();
    }
    public function login($data){
        $adminUserObj = new AdminUserModel();
        $adminUser = $this->getAdminUserByUsername($data['username']);
        if(!$adminUser){
            throw new \think\Exception("不存在该用户");
        }
        //判断密码是否正确
        if($adminUser['password'] != md5($data['password'].'_singwa_abc')){
            //return show('密码错误',config('status.error'));
            throw new \think\Exception("密码错误");
        }
        //需要记录信息到mysql中
        $updateData = [
            "last_login_time" => time(),
            "last_login_ip" => request()->ip(),
            "update_time" => time(),
        ];
        $res = $this->adminUserObj->updateById($adminUser["id"],$updateData);
        if(empty($res)){
            //return show('登录失败',config('status.error'));
            throw new \think\Exception("登录失败");
        }

        //记录Session
        session(config("admin.session_admin"),$adminUser);
        //return show('登录成功',config('status.success'));
        return true;
    }

    public function getAdminUserByUsername($username){
        $adminUser = $this->adminUserObj->getAdminUserByUsername($username);
        if(empty($adminUser) || $adminUser->status!=config("status.mysql.table_normal") ){
            //return show('不存在该用户',config('status.error'));
            //throw new \think\Exception("不存在该用户");
            return [];
        }
        $adminUser = $adminUser->toArray();
        return $adminUser;
    }
}